#include "driver/gpio.h"

#include "esp_log.h"
static const char *TAG = "bsp.led";

/* LED **************************************************************************************************/

#define LED_A_PIN  10
#define LED_B_PIN  11
#define LED_ACTIVE 0

typedef enum
{
    LED_RED = 0,
    LED_GREEN,
    LED_MAX
} led_type_t;

struct ledCtrl
{
    uint32_t onTime;
    uint32_t offTime;
    uint32_t allTime;
};

static uint32_t ledPin[LED_MAX] = {LED_A_PIN, LED_B_PIN};
static struct ledCtrl led[LED_MAX];

void bsp_led_toggle()
{
    static uint32_t counter[LED_MAX] = {0};

    for (int i = 0; i < LED_MAX; i++)
    {
        if (counter[i]++ < led[i].onTime)
        {
            gpio_set_level(ledPin[i], LED_ACTIVE);
        }
        else if (counter[i]++ < led[i].allTime)
        {
            gpio_set_level(ledPin[i], !LED_ACTIVE);
        }
        else
        {
            counter[i] = 0;
        }
    }
}

void bsp_set_led(led_type_t index, uint32_t onTime, uint32_t offTime)
{
    led[index].onTime = onTime;
    led[index].offTime = offTime;
    led[index].allTime = onTime + offTime;
}

void bsp_ledInit(void)
{
    ESP_LOGI(TAG, "configured to blink GPIO LED!");
    gpio_reset_pin(LED_A_PIN);
    gpio_reset_pin(LED_B_PIN);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(LED_A_PIN, GPIO_MODE_OUTPUT);
    gpio_set_direction(LED_B_PIN, GPIO_MODE_OUTPUT);

    bsp_set_led(LED_RED, 100, 100);
    bsp_set_led(LED_GREEN, 100, 100);
}